<?php
namespace app\store\controller;
use app\admin\common\Base;
use think\Db;

//序列号盘点
class Inventorysn extends Base
{
    public function index()
    {
        $data = $this->request->param();
       	//

       	 if (isset($data['ajax']) && $data['ajax'] == 100) {
            $map = '';
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);
            $is_sn_manager=$this->get_is_sn_manager_id();
  			//halt($is_sn_manager);
            if ($map == '') {
                $list = Db::name('stock_record_good')->where('is_del', 0)->where('good_id','in',$is_sn_manager)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->limit($start, $limit)->select();
                //$count = Db::name('stock_record_good')->where('is_del', 0)->where('good_id','in',$is_sn_manager)->where('enterprise_id', $this->EnterpriseId)->count();
            } else {
                $list = Db::name('stock_record_good')->where('is_del', 0)->where('good_id','in',$is_sn_manager)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->limit($start, $limit)->select();
                //$count = Db::name('stock_record_good')->where('is_del', 0)->where('good_id','in',$is_sn_manager)->where('enterprise_id', $this->EnterpriseId)->count();
            }
            $list1=[];
            $list2=[];
            foreach ($list as $key => &$val) {
            	$kk = $val['store_id'] . $val['good_id']; //分组标识
            	if (isset($list1[$kk])) {
            	
            		// $list1[$kk]['system_kc'] = $list1[$kk]['system_kc']+$val['number'];//系统库存
            		// $list1[$kk]['user_kc'] = 0;//实际库存
            		// $list1[$kk]['result'] = 0;//盘点结果

            	}else{
                    $list1[$kk]['store_id']=$val['store_id'];
                    $list1[$kk]['good_id']=$val['good_id'];
	            	$list1[$kk]['store_name'] = $val['store_id'] == 0 ? '' : Db::name('store')->where('id', $val['store_id'])->value('name');
	            	$g = Db::name('goods')->where('id', $val['good_id'])->find();
	                $list1[$kk]['good_name'] =  $g['name'];
	                $list1[$kk]['sn'] = $g['sn'];
	                $list1[$kk]['model'] = $g['model'];
	                $list1[$kk]['unit_name']=Db::name('unit')->where('id', $g['unit'])->value('name');
	                $list1[$kk]['good_category_name']=Db::name('goods_category')->where('id', $g['goods_category_id'])->value('name');
	                $number=Db::name('goods_sn')->where('store_id',$val['store_id'])->where('good_id',$val['good_id'])->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->count();
	                $list1[$kk]['system_kc'] = $number;//系统库存
            		$list1[$kk]['user_kc'] = 0;//实际库存
            		$list1[$kk]['result'] = "";//盘点结果

            	}
            	
             // $val['store_name'] = $val['store_id'] == 0 ? '' : Db::name('store')->where('id', $val['store_id'])->value('name');
             // $g = Db::name('goods')->where('id', $val['good_id'])->find();
             //    $val['good_name'] =  $g['name'];
             //    $val['sn'] = $g['sn'];
             //    $val['model'] = $g['model'];
             //    $val['unit_name']=Db::name('unit')->where('id', $g['unit'])->value('name');
             //    $val['good_category_name']=Db::name('goods_category')->where('id', $g['goods_category_id'])->value('name');

            // $val['store_name'] = $val['store_id'] == 0 ? '' : Db::name('store')->where('id', $val['store_id'])->value('name');
             //    $val['system_kc'] = 1;//系统库存
             //    $val['user_kc'] = 1;//实际库存
             //    $val['result'] = 1;//盘点结果
              
            }
            //自增序列号才能数据表格显示
            foreach ($list1 as $ke => $va) {
            	array_push($list2, $va);
            }
            //halt($list);
            $count=count($list2);
            return $this->grid_data($list2, $count, $page, $limit);
        }
        $today = date('Y-m-d', time());
        $mon_start = date('Y-m', time()) . '-01';
        $this->assign('today', $today);
        $this->assign('mon_start', $mon_start);
      
        return $this->fetch();
    }

    public function get_is_sn_manager_id(){
    	$list = Db::name('goods')->where('is_sn_manager', 1)->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->select();
    	if (empty($list)) {
    		return [];
    	}else{
    		$ids=array_column($list, 'id');
    		return $ids;
    	}
    } 
    //选择序列号
    public function sernumlist()
    {
        $data = $this->request->param();
        $this->assign('good_id',$data['good_id']);
        $this->assign('store_id',$data['store_id']);
        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $map = '';
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);
            
            if ($map == '') {
                $list = Db::name('goods_sn')->where('good_id',$data['good_id'])->where('store_id',$data['store_id'])->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->limit($start, $limit)->select();
                $count = Db::name('goods_sn')->where('good_id',$data['good_id'])->where('store_id',$data['store_id'])->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->count();
            } else {
                $list = Db::name('goods_sn')->where('good_id',$data['good_id'])->where('store_id',$data['store_id'])->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->limit($start, $limit)->select();
                $count = Db::name('goods_sn')->where('good_id',$data['good_id'])->where('store_id',$data['store_id'])->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->count();
  
            }
            return $this->grid_data($list, $count, $page, $limit);
        }
        return $this->fetch();
    }

    // 盘点结果
    public function sninventoryresult()
    {
        $data = $this->request->param();
        $this->assign('good_id',$data['good_id']);
        $this->assign('store_id',$data['store_id']);
        $new_sn=explode(',',$data['sns']);
        $system_sn=explode(',',$data['systemSns']);

        $this->assign('system_sn',$data['systemSns']);
        $this->assign('new_sn',$data['sns']);

        $addNum=0;
        $delNum=0;
        $rightNum=0;

        $del_sn=[];
        foreach($system_sn as $ke => $val){
            if(!in_array($val,$new_sn)){
                //  存被删除的sn
                array_push($del_sn,$val);

                array_push($new_sn,$val);

            }
        }

        foreach ($new_sn as $key => $value) {
            $list[$key]['sn']=$value;
            $flag=0;
            if(in_array($value,$del_sn)){
                //   删除的状态
                $flag=3;
                $delNum+=1;
            }else{
                if(in_array($value,$system_sn)){
                    //  在老数组中找到sn
                    $flag=1;
                    $rightNum+=1;
                }else{
                    //   新增的状态
                    $flag=2;
                    $addNum+=1;
                        
                }
            }
            $list[$key]['state']=$flag;

        }

        $this->assign('addNum',$addNum);
        $this->assign('delNum',$delNum);
        $this->assign('rightNum',$rightNum);
        

        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $map = '';
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);
            
            

            $count=count($list);

            

            return $this->grid_data($list, $count, $page, $limit);
        }
       

        return $this->fetch();
    }

}
